﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SPMS.Commons.Logging
{
    /// <summary>
    /// 简单日志工厂
    /// </summary>
    /// <remarks>用于提供默认的日志工厂实现</remarks>
    /// <history>
    /// [zanezeng]               2009/9/22 14:01    创建
    /// </history>
    public class SimpleLogFactory : ILogFactory
    {
        #region ---Constructor

        /// <summary>
        /// 初始化类型<see cref="SimpleLogFactory"/>
        /// </summary>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/12/22 9:19    创建
        /// </history>
        static SimpleLogFactory()
        {
            //设置日志级别
            s_Level = LogLevelEnum.Warn;
        }

        #endregion

        #region ---ILogFactory

        /// <summary>
        /// 获得给定名称的日志对象
        /// </summary>
        /// <param name="logName">要获得的日志对象的名称</param>
        /// <returns>获得的给定名称的日志对象</returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/22 13:40    创建
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/22 14:01    创建
        /// </history>
        public ILog GetLog( string logName )
        {
            //判断日志对象是否创建
            if (null != s_SimpleLog)
            {
                //返回日志对象
                return s_SimpleLog;
            }

            //同步
            lock (DEFAULT_FACTORY)
            {
                //判断日志对象是否创建
                if (null != s_SimpleLog)
                {
                    //返回日志对象
                    return s_SimpleLog;
                }

                //创建日志对象
                s_SimpleLog = new SimpleLog( Level );
            }

            //返回日志对象
            return s_SimpleLog;
        }

        #endregion

        #region ---Property

        /// <summary>
        /// 获得默认工厂实例
        /// </summary>
        /// <value>默认工厂实例</value>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/29 10:41    创建
        /// </history>
        public static SimpleLogFactory Default
        {
            get
            {
                return DEFAULT_FACTORY;
            }
        }

        /// <summary>
        /// 获得或者设置允许输出的日志级别
        /// </summary>
        /// <value>允许输出的日志级别</value>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/29 16:29    创建
        /// </history>
        public static LogLevelEnum Level
        {
            get
            {
                return s_Level;
            }
            set
            {
                //记录日志级别
                s_Level = value;

                //判断日志记录器是否已经创建
                lock (DEFAULT_FACTORY)
                {
                    //判断日志记录器是否已经创建
                    if (null != s_SimpleLog)
                    {
                        //设置日志级别
                        s_SimpleLog.Level = value;
                    }
                }
            }
        }

        #endregion

        #region ---Field

        /// <summary>
        /// 简单日志
        /// </summary>
        private static SimpleLog s_SimpleLog;

        /// <summary>
        /// 日志级别
        /// </summary>
        private static LogLevelEnum s_Level;

        /// <summary>
        /// 默认工厂实例
        /// </summary>
        private readonly static SimpleLogFactory DEFAULT_FACTORY = new SimpleLogFactory();

        #endregion
    }
}
